﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GongXianDCUI.Models;
using GongXianDCUI.CommonLib;

namespace GongXianDCUI.DAL
{
    public class ArticleDAL
    {
        /// <summary>
        /// 分页，获取子菜单模块下的所有文章
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public List<Article> GetList(int categoryId, int state, int pageIndex, int pageSize, out decimal total)
        {
            List<Article> list = new List<Article>();
            StringBuilder sb = new StringBuilder();
            string sqlWhere = string.Empty;
            if (categoryId > 0)
            {
                sqlWhere = " categoryId=" + categoryId;
            }
            sqlWhere += " and state=" + state;

            System.Data.DataTable dt = SQLHepler.GetPaging2008("pubdate desc", "Article", sqlWhere, pageIndex, pageSize, out total);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var item = dt.Rows[i];
                    Article model = new Article();
                    model.articleId = item["articleId"] is DBNull ? 0 : Convert.ToInt64(item["articleId"]);
                    model.categoryId = item["categoryId"] is DBNull ? 0 : Convert.ToInt32(item["categoryId"]);
                    model.title = item["title"] is DBNull ? string.Empty : Convert.ToString(item["title"]);
                    //if (i == 0)
                    //{
                    //    model.content = item["content"] is DBNull ? string.Empty : Convert.ToString(item["content"]);
                    //}
                    model.issuer = item["issuer"] is DBNull ? string.Empty : Convert.ToString(item["issuer"]);
                    model.pubdate = Convert.ToDateTime(item["pubdate"]);
                    model.state = item["state"] is DBNull ? 0 : Convert.ToInt32(item["state"]);
                    model.hits = item["hits"] is DBNull ? 0 : Convert.ToInt32(item["hits"]);
                    model.newFlag = item["newFlag"] is DBNull ? string.Empty : Convert.ToString(item["newFlag"]);
                    model.hotFlag = item["hotFlag"] is DBNull ? string.Empty : Convert.ToString(item["hotFlag"]);
                    model.picFlag = item["picFlag"] is DBNull ? string.Empty : Convert.ToString(item["picFlag"]);
                    model.topFlag = item["topFlag"] is DBNull ? string.Empty : Convert.ToString(item["topFlag"]);
                    model.Privilege = item["Privilege"] is DBNull ? string.Empty : Convert.ToString(item["Privilege"]);
                    list.Add(model);
                }
            }
            return list;
        }

        /// <summary>
        /// 分页，获取首页导航大模块下的所有文章
        /// </summary>
        /// <param name="functionId">大模块ID</param>
        /// <param name="state">发布状态，1=已发布，2=未发布</param>
        /// <returns></returns>
        public List<Article> GetModelAllArticleList(int functionId, int state, int pageIndex, int pageSize, out decimal total)
        { 
            List<Article> list = new List<Article>();
            StringBuilder sb = new StringBuilder();
            string sqlWhere = string.Empty;
            if (functionId > 0)
            {
                sqlWhere = " categoryId like '" + functionId + "%'";
            }
            sqlWhere += " and state=" + state;

            System.Data.DataTable dt = SQLHepler.GetPaging2008("pubdate desc", "Article", sqlWhere, pageIndex, pageSize, out total);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var item = dt.Rows[i];
                    Article model = new Article();
                    model.articleId = item["articleId"] is DBNull ? 0 : Convert.ToInt64(item["articleId"]);
                    model.categoryId = item["categoryId"] is DBNull ? 0 : Convert.ToInt32(item["categoryId"]);
                    model.title = item["title"] is DBNull ? string.Empty : Convert.ToString(item["title"]);
                    model.issuer = item["issuer"] is DBNull ? string.Empty : Convert.ToString(item["issuer"]);
                    model.pubdate = Convert.ToDateTime(item["pubdate"]);
                    model.state = item["state"] is DBNull ? 0 : Convert.ToInt32(item["state"]);
                    model.hits = item["hits"] is DBNull ? 0 : Convert.ToInt32(item["hits"]);
                    model.newFlag = item["newFlag"] is DBNull ? string.Empty : Convert.ToString(item["newFlag"]);
                    model.hotFlag = item["hotFlag"] is DBNull ? string.Empty : Convert.ToString(item["hotFlag"]);
                    model.picFlag = item["picFlag"] is DBNull ? string.Empty : Convert.ToString(item["picFlag"]);
                    model.topFlag = item["topFlag"] is DBNull ? string.Empty : Convert.ToString(item["topFlag"]);
                    model.Privilege = item["Privilege"] is DBNull ? string.Empty : Convert.ToString(item["Privilege"]);
                    list.Add(model);
                }
            }
            return list;
        }

        public Article GetModel(long articleId)
        {
            Article model = DB.Context.From<Article>().Where(w => w.articleId == articleId).First();
            return model;
        }



    }
}
